|
Download
FAQ History |
|
API
Search Feedback |
The Example Servlets
This chapter uses the Duke's Bookstore application to illustrate the tasks involved in programming servlets. Table 11-1 lists the servlets that handle each bookstore function. Each programming task is illustrated by one or more servlets. For example,
BookDetailsServletillustrates how to handle HTTPGETrequests,BookDetailsServletandCatalogServletshow how to construct responses, andCatalogServletillustrates how to track session information.
The data for the bookstore application is maintained in a database and accessed through the helper class
database.BookDB. Thedatabasepackage also contains the classBookDetails, which represents a book. The shopping cart and shopping cart items are represented by the classescart.ShoppingCartandcart.ShoppingCartItem, respectively.The source code for the bookstore application is located in the
<INSTALL>/j2eetutorial14/examples/web/bookstore1/directory created when you unzip the tutorial bundle (see Building and Running the Examples). A samplebookstore1.waris provided in<INSTALL>/j2eetutorial14/examples/web/provided-wars/. To build, package, deploy, and run the example:
- Build and package the bookstore common files as described in Duke's Bookstore Examples.
- In a terminal window, go to
<INSTALL>/j2eetutorial14/examples/web/bookstore1/.- Run
asantbuild. This target will spawn any necessary compilations and copy files to the<INSTALL>/j2eetutorial14/examples/web/bookstore1/build/directory.- Start the J2EE application server.
- Perform all the operations described in Accessing Databases from Web Applications.
- Start
deploytool.- Create a Web application called
bookstore1by running the New Web Application Wizard. Select FileNew
Web Application WAR.
- New Web Application Wizard
- Select the Create New Stand-Alone WAR Module radio button.
- Click Browse and in the file chooser, navigate to
<INSTALL>/docs/tutorial/examples/web/bookstore1/build/.- In the File Name field, enter
bookstore1.- Click Choose Module File.
- In the WAR Display Name field, enter
bookstore1.- In the Context Root field, enter
/bookstore1.- Click Edit.
- In the Edit Archive Contents dialog box, navigate to
<INSTALL>/j2eetutorial14/examples/web/bookstore1/build/. Selecterrorpage.html,duke.books.gif,BannerServlet.class,BookStoreServlet.class,BookDetailsServlet.class,CatalogServlet.class,ShowCartServlet.class,CashierServlet.class,ReceiptServlet.class,and thedatabase,filters,listeners, andutilpackages. Click Add.- Add the shared bookstore library. Navigate to
<INSTALL>/j2eetutorial14/examples/build/web/bookstore/dist/. Selectbookstore.jarand Click Add.- Click OK.
- Click Next.
- Select the Servlet radio button.
- Click Next.
- Select
BannerServletfrom the Servlet Class combo box.- Click Finish.
- Add each of the Web components listed in Table 11-2. For each servlet:
- Select File
New
Web Application WAR.
- Click the Add to Existing WAR Module radio button. Since the WAR contains all of the servlet classes, you do not have to add any more content.
- Click Next.
- Select the Servlet radio button and the Component Aliases checkbox.
- Click Next.
- Select the servlet from the Servlet Class combo box.
- Click Next.
- Click Add. Enter the alias.
- Click Finish.
- Add the listener class
listeners.ContextListener(described in Handling Servlet Life Cycle Events).- Add an error page (described in Handling Errors).
- Add the filters
filters.HitCounterFilterandfilters.OrderFilter(described in Filtering Requests and Responses).
- Select the Filter Mapping tab.
- Click Edit Filter List.
- Click Add.
- Select
filters.HitCounterFilterfrom the Filter Class column. The deploytool will automatically enterHitCounterFilterin the Display Name column.- Click Add.
- Select
filters.OrderFilterfrom the Filter Class column. The deploytool will automatically enterOrderFilterin the Display Name column.- Click OK.
- Click Add.
- Select
HitCounterFilterfrom the Filter Name column.- Select Servlet from the Target Type column.
- Select
BookStoreServletfrom the Target column.- Repeat for
OrderFilter. The target type is Servlet and the target isReceiptServlet.- Add a resource reference for the database.
- Select the Resource Refs tab.
- Click Add.
- Enter
jdbc/BookDBin the Coded Name field.- Accept the default type
javax.sql.DataSource.- Accept the default authorization
Container.- Accept the default selected
Shareable.- Enter
jdbc/BookDBin the JNDI name field of the Deployment setting for jdbc/BookDB frame.- Select File
Save.
- Deploy the application.
- To run the application, open the bookstore URL
http://localhost:1024/bookstore1/bookstore.Troubleshooting
The Duke's Bookstore database access object returns the following exceptions:
BookNotFoundException--Returned if a book can't be located in the bookstore database. This will occur if you haven't loaded the bookstore database with data by runningasantcreate-db_commonor if the database server hasn't been started or it has crashed.BooksNotFoundException--Returned if the bookstore data can't be retrieved. This will occur if you haven't loaded the bookstore database with data or if the database server hasn't been started or it has crashed.UnavailableException--Returned if a servlet can't retrieve the Web context attribute representing the bookstore. This will occur if the database server hasn't been started.Because we have specified an error page, you will see the message
The application is unavailable. Please try later. If you don't specify an error page, the Web container generates a default page containing the messageA Servlet Exception Has Occurredand a stack trace that can help diagnose the cause of the exception. If you useerrorpage.html, you will have to look in the server log to determine the cause of the exception. The server log file is <J2EE_HOME>/domains/domain1/server/logs/server.log.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.